use sea_orm_migration::prelude::*;

#[derive(DeriveMigrationName)]
pub struct Migration;

#[async_trait::async_trait]
impl MigrationTrait for Migration {
    async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
        // 分别添加每一列，显式指定默认值
        manager
            .alter_table(
                Table::alter()
                    .table(SpiderTask::Table)
                    .add_column(
                        ColumnDef::new(SpiderTask::ArticleListSelector)
                            .string_len(256)
                            .default("")
                            .not_null()
                    )
                    .to_owned(),
            )
            .await?;

        manager
            .alter_table(
                Table::alter()
                    .table(SpiderTask::Table)
                    .add_column(
                        ColumnDef::new(SpiderTask::ArticleTitleSelector)
                            .string_len(256)
                            .default("")
                            .not_null()
                    )
                    .to_owned(),
            )
            .await
    }

    async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
        // 分别删除每一列
        manager
            .alter_table(
                Table::alter()
                    .table(SpiderTask::Table)
                    .drop_column(SpiderTask::ArticleListSelector)
                    .to_owned(),
            )
            .await?;

        manager
            .alter_table(
                Table::alter()
                    .table(SpiderTask::Table)
                    .drop_column(SpiderTask::ArticleTitleSelector)
                    .to_owned(),
            )
            .await
    }
}

#[derive(DeriveIden)]
enum SpiderTask {
    Table,
    ArticleListSelector,
    ArticleTitleSelector,
}